package com.robot;

import java.awt.Color;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.robot.command.MsgDispatcher;

import iqq.im.QQException;
import iqq.im.actor.ThreadActorDispatcher;
import iqq.im.bean.QQMsg;
import iqq.im.event.QQNotifyEvent;
import iqq.im.event.QQNotifyEventArgs;
import iqq.im.event.QQNotifyHandler;
import iqq.im.event.QQNotifyHandlerProxy;

public class RobotApp {
	private RobotClient client;
	private MsgDispatcher msgDispatcher;
	RobotApp(){
		init();
	}

	private void init() {
		this.client = new RobotClient(null, null,new QQNotifyHandlerProxy(this),
				new ThreadActorDispatcher());
		this.msgDispatcher = new MsgDispatcher(client);
		
	}

	
	/**
	 * 被踢下线通知
	 */
	@QQNotifyHandler(QQNotifyEvent.Type.KICK_OFFLINE)
	private void processKickOff(QQNotifyEvent event){
		System.out.println("被踢下线: " + (String)event.getTarget());
		client.showMsg("被踢下线: " + (String)event.getTarget(), Color.RED);
	}
	
	/**
	 * 需要验证码通知
	 */
	@QQNotifyHandler(QQNotifyEvent.Type.CAPACHA_VERIFY)
	private void processVerify(QQNotifyEvent event) throws IOException{
		QQNotifyEventArgs.ImageVerify verify = (QQNotifyEventArgs.ImageVerify) event.getTarget();
		System.out.print("请输入在项目根目录下verify.png图片里面的验证码:");
		String code = JOptionPane.showInputDialog(new ImageIcon(verify.image),"请输入验证码");
		if(code!=null){
			client.submitVerify(code, event);			
		}
	}
	/**
	 * 聊天消息通知，使用这个注解可以收到QQ消息
	 * @throws Exception 
	 */
	@QQNotifyHandler(QQNotifyEvent.Type.CHAT_MSG)
	public void processBuddyMsg(QQNotifyEvent event) throws QQException,Exception{
		final QQMsg msg = (QQMsg) event.getTarget();
		msgDispatcher.pushMsg(msg);
		
	}
	public static void main(String[] args) {
		new RobotApp();
	}
}
